
TOPDIR := $(PWD)/..
LIBS_DIR := $(TOPDIR)/src

TARGET := $(PWD)/_build/test.elf
V :=@

CROSS_PREFIX := aarch64-linux-gnu-
export CROSS_PREFIX

ORIGIN_BORAD ?= 0
FVP_BUILD ?= 0

ifeq ($(ORIGIN_BORAD), 1)
CFLAGS += -DORIGIN_BORAD

ifeq ($(FVP_BUILD),1)
CFLAGS += -DBOARD_FVP_AEM_BASE
else
CFLAGS += -DRELOCATE_APP
endif

boot-y := ../src/board/fm/boot/start.o
boot-y += ../src/board/fm/boot/excpt.o
LIBS-y += $(LIBS_DIR)/board/fm/boot/
LIBS-y += $(LIBS_DIR)/lib/
LIBS-y += $(LIBS_DIR)/board/lib/
endif #end ORIGIN_BORAD


CFLAGS += -I $(TOPDIR)/src/include
LIBS-y += $(LIBS_DIR)/app/

# test-y += main.o

#the include must under LIBS-y, over other
include $(TOPDIR)/script/Makefile.test

.dumy := $(shell [ -e _build ] || mkdir _build)

# all:
build:
	make clean
	make -f Makefile aem
	make clean
	make -f Makefile gem

# export LIBS
# aem: $(boot-y) $(LIBS) $(test-y)
# 	make -C $(TOPDIR)/src/aem APP=$(TARGET).$@
# 	fromelf -c $(TARGET).$@ >$(TARGET).$@.asm

fvp: build_version
	make -f Makefile _aem FVP_BUILD=1 ORIGIN_BORAD=1

aem: build_version
	make -f Makefile _aem ORIGIN_BORAD=1

_aem: $(boot-y) $(LIBS) $(test-y)
	$(LD) -Taem.lds  -static $^ -o $(TARGET).$@
	# $(V) $(CC) -static -fno-builtin-_start $^ -o $(TARGET).$@ 
	$(OBJCOPY) -O binary $(TARGET).$@ $(TARGET).$@.bin
	$(OBJDUMP) -D $(TARGET).$@ > $(TARGET).$@.asm

gem: $(LIBS) $(test-y)
	$(CC) -static $^ -o $(TARGET).$@	


build_version:
	echo '#define BUILD_VERSION "bare'`date +"%y%m%d%H%M%S"`'"' > ../src/include/build.h